package com.monolith.jd.demo.controller;

import com.alibaba.fastjson.JSON;
import com.monolith.jd.demo.common.entity.PackageEntity;
import com.monolith.jd.demo.common.resource.ImageData;
import com.monolith.jd.demo.common.vo.*;
import com.monolith.jd.demo.conf.JdApiConfiguration;
import com.monolith.jd.demo.service.PackageService;
import com.monolith.jd.demo.ws.cache.WebsocketSessionCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2018/10/1 Create by 郭文梁
 * ActionController
 * Recommend of this class
 *
 * @author 郭文梁
 * @date 2018/10/1
 */
@RestController
@RequestMapping("/")
@Slf4j
public class ActionController {

    private final JdApiConfiguration jdApiConfiguration;
    private final ImageData imageData;
    private final PackageService packageService;
    private final WebsocketSessionCache websocketSessionCache;

    @Autowired
    public ActionController(JdApiConfiguration jdApiConfiguration, ImageData imageData, PackageService packageService, WebsocketSessionCache websocketSessionCache) {
        this.jdApiConfiguration = jdApiConfiguration;
        this.imageData = imageData;
        this.packageService = packageService;
        this.websocketSessionCache = websocketSessionCache;
    }

    @PostMapping("execute")
    public JdResponseVo execute(
            @RequestBody byte[] body) {
        boolean success = saveData(body);
        if (success) {
            return getSuccessResponse();
        } else {
            return getFailResponse(1, "保存失败");
        }
    }

    @GetMapping("info")
    public JdApiConfiguration info() {
        return jdApiConfiguration;
    }

    @GetMapping("/list")
    public Map<String, Object> list() {
        Map<String, Object> result = new HashMap<>(2);
        long count = packageService.getCount();
        List<PackageEntity> data = packageService.list();
        result.put("count", count);
        result.put("data", data);
        return result;
    }

    @PostMapping("/clear")
    public BasicJsonResult<String> clear() {
        packageService.clear();
        return BasicJsonResult.ok();
    }


    private boolean saveData(byte[] body) {
        String json = new String(body);
        JdRequestVo requestVo = new JdRequestVo().loadData(json);
        String requestDataJson = requestVo.getData();
        JdRequestData data = new JdRequestData().loadData(requestDataJson);
        websocketSessionCache.send2AllSession(JSON.toJSONString(data));
        return packageService.saveData(data);
    }

    private JdResponseVo getFailResponse(int code, String msg) {
        JdResponseVo responseVo = new JdResponseVo();
        responseVo.setCode(code);
        responseVo.setMessageCode(code);
        responseVo.setMessage(msg);
        return responseVo;
    }

    private JdResponseVo getSuccessResponse() {
        JdResponseData data = new JdResponseData();
        data.setPackageCode("code for package");
        data.setPackageImgStr(imageData.getBase64String());

        JdResponseVo responseVo = new JdResponseVo();
        responseVo.setCode(200);
        responseVo.setMessageCode(200);
        responseVo.setMessage("操作成功");
        responseVo.setData(data.toJSON());
        return responseVo;
    }
}
